class Cache {
  constructor(){
    this.data = {}
  }
  //设置缓存
  setCache(key,value){
    if(!key)return
    if(!value)return
    let data = JSON.stringify(value)
    data =  JSON.parse(data)
    if(this.data[key]){
      this.data[key] = {
        value:data,
        once:false
      }
    }else{
      this.data[key] = {
        value:data,
        once:false
      }
    }
  }
  //设置读取一次后失效的缓存
  setOnceCache(key,value){
    if(!key)return
    if(!value)return
    let data = JSON.stringify(value)
    data =  JSON.parse(data)
    if(this.data[key]){
      this.data[key] = {
        value:data,
        once:true
      }
    }else{
      this.data[key] = {
        value:data,
        once:true
      }
    }
  }
  //获取缓存
  getCache(key){
    let data = this.data[key]
    if(data){
      if(data.once){
        data = JSON.stringify(data)
        this.data[key] = null 
        data = JSON.parse(data)
        return data.value
      }else{
        return data.value
      }
    }else{
      return null
    }
  }
}
let $cache = new Cache()
module.exports = { $cache }